package com.tron.dobest.mybatisplus.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tron.dobest.mybatisplus.entity.Role;
import com.tron.dobest.mybatisplus.entity.User;
import com.tron.dobest.mybatisplus.vo.AccessVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    String JOIN = " join ";
    String ON = " on ";
    String CUSTOM_SQL_SEGMENT = " ${ew.customSqlSegment} ";

    String queryRole = "select role.* from user" +
            JOIN +
            "user_role" +
            ON +
            "user.id = user_role.user_id" +
            JOIN +
            "role" +
            ON +
            "user_role.role_id = role.id" +
            CUSTOM_SQL_SEGMENT;

    //"select role.* from user join user_role on user.id = user_role.user_id join role on user_role.role_id = role.id ${ew.customSqlSegment}"
    @Select(queryRole)
    List<Role> getInfo(@Param(Constants.WRAPPER) QueryWrapper<?> wrapper);

    @Select("select role.* from user join user_role on user.id = user_role.user_id join role on user_role.role_id = role.id ${ew.customSqlSegment}")
    Page<Role> getPage(@Param(Constants.WRAPPER) QueryWrapper<?> wrapper, Page page);


    @Select("SELECT access.* FROM role JOIN organization_role ON role.id = organization_role.role_id JOIN role_access ON role.id = role_access.role_id JOIN access ON role_access.access_id = access.id UNION ALL SELECT access.* FROM role JOIN user_role ON role.id = user_role.role_id JOIN role_access ON role.id = role_access.role_id JOIN access ON role_access.access_id = access.id UNION ALL SELECT access.* FROM role JOIN department_role ON role.id = department_role.role_id JOIN role_access ON role.id = role_access.role_id JOIN access ON role_access.access_id = access.id ${ew.customSqlSegment}")
    List<AccessVO> getAccessList(@Param(Constants.WRAPPER) QueryWrapper<?> wrapper, Page page);


}
